package mo.golden;

import mo.common.ListNode;

public class L02_08 {

    public ListNode detectCycle(ListNode head) {
        ListNode slow = head;
        ListNode fast = head;
        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
            if (slow == fast) {
                ListNode res = head;
                while (res != fast) {
                    res = res.next;
                    fast = fast.next;
                }
                return res;
            }
        }
        return null;
    }

}
